package com.waitingsnow.modules.shell.view.baseView
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.model.vo.GiftVO;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.modules.shell.ShellFacade;
import com.waitingsnow.modules.shell.model.RubyServerProxy;
import com.waitingsnow.modules.shell.view.playGift.PlayGiftView;

import flash.display.Sprite;
import flash.events.Event;

/**
 * 
 * @author waitingsnow
 * $Id: GiftView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class GiftView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>GiftView</code> instance.
	 * 
	 */
	public function GiftView()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var proxy:RubyServerProxy;
	private var playGiftVIew:PlayGiftView;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		mouseEnabled = mouseChildren = false;
		proxy = ShellFacade.getInstance().getContent(RubyServerProxy.NAME) as RubyServerProxy;
		ExternalUtil.callback(AppConfig.PLAY_GIFT_TO_SWF, playGiftHandler);
		
		/*playGiftHandler(2, 50, 1);
		playGiftHandler(2, 50, 1);
		playGiftHandler(2, 52, 1);*/
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function playGiftHandler(giftID:uint, count:uint, type:int = 1):void
	{
		Logger.log("收到礼物id: " + giftID + " 礼物数量：" + count + " 礼物类型: " + type);
		if (count >= 4000)
		{
			count = 500;
		}
		else if (count >= 1000)
		{
			count = 400;
		}
		else if (count >= 500)
		{
			count = 300;
		}
		else if (count >= 200)
		{
			count = 200;
		}
		proxy.addGiftVo(giftID, count);
		playQueueGift();
	}
	
	private function playQueueGift():void
	{
		if (playGiftVIew && playGiftVIew.isRun)
		{
			return;
		}
		var giftVO:GiftVO = proxy.getCurGiftVO();
		if (giftVO)
		{
			if (!playGiftVIew)
			{
				playGiftVIew = new PlayGiftView();
				playGiftVIew.addEventListener(Event.COMPLETE, playGiftViewCompleteHandler);
				addChild(playGiftVIew);
			}
			playGiftVIew.playGift(giftVO, proxy.getCurGiftNum());
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function playGiftViewCompleteHandler(event:Event):void
	{
		if (!playGiftVIew.isRun)
		{
			playQueueGift();
		}
	}
}
}